if (typeof gxc == "undefined") {
	var gxc = {};
}

// document初始化
jQuery(function() {
	gxc.dialog.reLocatePromp();
	jQuery(window).resize(function() {
		gxc.dialog.reLocatePromp();
	});
})

/**
 * id为每个dialog的序号，递增
 */
gxc.dialog = {
	id : 1
};

/**
 * 提示弹出层
 */
gxc.dialog.hidePrompt = function(id) {
	jQuery("#" + id).slideToggle("fast", function() {
		jQuery("#" + id).remove();
	});
}

gxc.dialog.prompt = function(text, timeout) {

	var div_id = "gxc_dialog_prompt_" + gxc.dialog.id;
	gxc.dialog.id++;

	if (jQuery("#gxc_promp_area").length < 1) {
		jQuery("body").append("<div id='gxc_promp_area'></div>");
		gxc.dialog.reLocatePromp();
	}
	var htmlString = "<div class='gxc_promp_item' id='" + div_id
			+ "' style='display: none;'>";
	htmlString += text;
	htmlString += "</div>";

	jQuery("#gxc_promp_area").append(htmlString);
	jQuery("#" + div_id).slideToggle("fast");

	// 设置timeout时间后隐藏
	var promp_timeout = 3000;
	if (typeof timeout == "number") {
		promp_timeout = timeout;
	}
	setTimeout(function() {
		gxc.dialog.hidePrompt(div_id);
	}, promp_timeout);
}

gxc.dialog.reLocatePromp = function() {
	var width = document.documentElement.clientWidth;
	var div_width = jQuery("#gxc_promp_area").width();
	var offset_left = width / 2 - div_width / 2;
	jQuery("#gxc_promp_area").css("left", offset_left);
}

/**
 * params属性包括：title,content,iframe,isModal（是否模态,备用，先默认为模态）,width,height,callback(关闭时调用)
 */
gxc.dialog.create = function(params) {
	gxc.dialog.id++;
	var div_id = "gxc_dialog_" + gxc.dialog.id;
	var bg_id = "gxc_modal_bg_" + gxc.dialog.id;

	if (typeof gxc.dialog.dialogObj != "object") {
		gxc.dialog.dialogObj = {};
	}
	gxc.dialog.dialogObj[div_id] = {};

	// 外层框架
	var htmlString = "";
	if (typeof params.isModal != "boolean") {
		params.isModal = true;
	}
	if (params.isModal) {
		htmlString += "<div class='gxc_modal_background' id='" + bg_id
				+ "' style='display:none;'></div>";
	}
	htmlString += "<div class='gxc_dialog' id='" + div_id + "' style='width:"
			+ params.width + "px;height:" + params.height
			+ "px;' onmousedown='gxc.dialog.dialogObj." + div_id
			+ ".setTopLayer()'>";

	// 标题
	htmlString += "<div class='gxc_dialog_title'><span>" + params.title
			+ "</span>"
			+ "<div class='close_btn' onclick='gxc.dialog.dialogObj." + div_id
			+ ".close()'></div>"
			+ "<div class='maximize_btn' onclick='gxc.dialog.dialogObj."
			+ div_id + ".maxSize()'></div>"
			+ "<div class='normalsize_btn' onclick='gxc.dialog.dialogObj."
			+ div_id + ".normalSize()'></div>" + "</div>";
	// 内容
	htmlString += "<div class='gxc_dialog_content'>";
	if (typeof params.iframe == "string") {
		htmlString += "<iframe src='" + params.iframe + "'><iframe>";
	} else {
		htmlString += "<div class='gxc_dialog_content_string'>"
				+ params.content + "</div>";
	}
	htmlString += "</div></div>";
	// 添加到html
	jQuery("body").append(htmlString);

	// 设置显示位置
	var clientWidth = document.documentElement.clientWidth;
	var clientHeight = document.documentElement.clientHeight;
	var theObj = jQuery("#" + div_id);
	if (theObj.width() < clientWidth) {
		theObj.css("left", (clientWidth - theObj.width()) / 2 + "px");
	}
	if (theObj.height() < clientHeight) {
		theObj.css("top", (clientHeight - theObj.height()) / 3 + "px");
	}
	gxc.dialog.dialogObj[div_id].bgObj = jQuery("#" + bg_id);
	gxc.dialog.dialogObj[div_id].divId = div_id;
	gxc.dialog.dialogObj[div_id].divObj = theObj;
	gxc.dialog.dialogObj[div_id].width = theObj.width();
	gxc.dialog.dialogObj[div_id].height = theObj.height();
	gxc.dialog.dialogObj[div_id].left = theObj.offset().left;
	gxc.dialog.dialogObj[div_id].top = theObj.offset().top;

	// 设置对话框内容尺寸
	gxc.dialog.dialogObj[div_id].resize = function() {
		var clientWidth = document.documentElement.clientWidth;
		var clientHeight = document.documentElement.clientHeight;
		var titleHeight = this.divObj.find(".gxc_dialog_title").height();
		this.divObj.find(".gxc_dialog_content").height(
				this.divObj.height() - titleHeight - 1);
	}

	// 显示对话框
	gxc.dialog.dialogObj[div_id].show = function() {
		this.resize();
		this.bgObj.show();
		this.divObj.fadeIn("fast");
	}

	// 隐藏对话框
	gxc.dialog.dialogObj[div_id].hide = function() {
		this.bgObj.hide();
		this.divObj.fadeOut("fast");
	}

	// 关闭对话框
	gxc.dialog.dialogObj[div_id].close = function() {
		this.divObj.fadeOut("fast");
		this.hide();
		var dialog = this;
		setTimeout(function() {
			dialog.bgObj.remove();
			dialog.divObj.remove();
			dialog = null;
			gxc.dialog.dialogObj[div_id] = null;
		}, 1000);
	}

	// 最大化
	gxc.dialog.dialogObj[div_id].maxSize = function() {
		// 记录最大化前的dialog状态
		this.width = this.divObj.width();
		this.height = this.divObj.height();
		this.left = this.divObj.offset().left;
		this.top = this.divObj.offset().top;
		// 最大化
		this.divObj.css("width", "100%");
		this.divObj.css("height", "100%");
		this.divObj.css("left", "0");
		this.divObj.css("top", "0");
		this.resize();
		this.divObj.find(".maximize_btn").hide();
		this.divObj.find(".normalsize_btn").show();
		// 可拖拽
		this.setDraggable(false);
	}

	// 还原
	gxc.dialog.dialogObj[div_id].normalSize = function() {
		this.divObj.css("width", this.width + "px");
		this.divObj.css("height", this.height + "px");
		this.divObj.css("left", this.left + "px");
		this.divObj.css("top", this.top + "px");
		this.resize();
		this.divObj.find(".normalsize_btn").hide();
		this.divObj.find(".maximize_btn").show();
		// 可拖拽
		this.setDraggable(true);
	}

	// 设置拖拽
	gxc.dialog.dialogObj[div_id].setDraggable = function(isDraggable) {
		if (isDraggable) {
			this.divObj.draggable({
				handle : ".gxc_dialog_title",
				cursor : "move",
				containment : "window"
			});
		} else {
			this.divObj.draggable("destroy");
		}
	}

	// 设置dialog层次
	gxc.dialog.dialogObj[div_id].setTopLayer = function() {
		if (!this.divObj.hasClass("top_dialog")) {
			jQuery(".gxc_dialog.top_dialog").removeClass("top_dialog");
			this.divObj.addClass("top_dialog");
		}
	}

	gxc.dialog.dialogObj[div_id].setDraggable(true);
	gxc.dialog.dialogObj[div_id].show();
}
